﻿using Sundry.App_Start.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Sundry.Areas.WebApi.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public class ApiAdminMenuController : ApiController
    {
        // GET api/apiadminmenu
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="model">菜单类(获取列表)</param>
        /// <returns>返回菜单列表</returns>
        [HttpPost]
        [Sundry.App_Start.WebApi.HttpValidateModel]
        public HttpResponseMessage GetAdminMenuList([FromBody]Sundry.Areas.WebApi.Models.AdminMenuModel model){
            List<Expression<Func<Sundry.Model.AdminMenu, bool>>> whereLamdaList = new List<Expression<Func<Model.AdminMenu,bool>>> ();
            // 所属父级菜单Id(-10忽略)
            if (model.ParentMenuId != -10) {
                whereLamdaList.Add(m => m.ParentMenuId == model.ParentMenuId);
            }
            if (!string.IsNullOrEmpty(model.MenuName)) {
                whereLamdaList.Add(m => m.MenuName.Contains(model.MenuName));
            }
            // 是否属于管理员菜单(-10忽略)
            if (model.IsAdmin != -10) {
                whereLamdaList.Add(m => m.IsAdmin == model.IsAdmin);
            }
            // 是否显示(-10忽略)
            if (model.IsShow != -10) {
                whereLamdaList.Add(m => m.IsShow == model.IsShow);
            }
            // 是否属于店铺菜单(-10忽略)
            if (model.IsStoreAdmin != -10) {
                whereLamdaList.Add(m => m.IsStoreAdmin == model.IsStoreAdmin);
            }
            //排序条件
            Dictionary<Expression<Func<Sundry.Model.AdminMenu, int>>, bool> orderDic = new Dictionary<Expression<Func<Model.AdminMenu,int>>,bool>();
            orderDic.Add(m => m.ParentMenuId, false);
            orderDic.Add(m => m.AdminOrderNum, false);
            orderDic.Add(m => m.StoreOrderNum, false);

            int rowCount = 0;

            var query= EntryService.GetServiceEntrace().GetAdminMenuService.GetAdminMenu(whereLamdaList, orderDic, model.StartNum, model.PageSize, out rowCount).ToList();
            return Sundry.App_Start.WebApi.HttpMessage.JsonFormat(new {
                state =query.Count>0? "success":"empty",
                TotalRecords = query.Count,
                AdminMenuList = query
            });
        }
    }
}
